		
		package no.hiof.servercommunication;
		
		import java.net.URI;
		import java.util.ArrayList;
		import java.util.List;
		import java.sql.Timestamp;
		
		import org.apache.http.HttpResponse;
		import org.apache.http.HttpVersion;
		import org.apache.http.NameValuePair;
		import org.apache.http.client.HttpClient;
		import org.apache.http.client.entity.UrlEncodedFormEntity;
		import org.apache.http.client.methods.HttpPost;
		import org.apache.http.impl.client.DefaultHttpClient;
		import org.apache.http.message.BasicNameValuePair;
		import org.apache.http.params.CoreProtocolPNames;
		
		import com.google.gson.Gson;
		import com.norseman.client.android.communicator.exception.NetworkException;
		import com.norseman.client.android.communicator.exception.ServerException;
		
		import no.hiof.servercommunication.receive.beans;*;
			
		public class RestFulServiceManager {
			private static HttpClient httpClient;
			private static HttpPost httppost;
			private static Gson gson;
						
			private static RestFulServiceManager instance;
			
			public static final String Klimatur_URL = "http://klimatur.hiof.no:8887/norsemanserver";
			
		    private RestFulServiceManager() {
		        httpClient = new DefaultHttpClient();
		        httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
		                	                
				httppost = new HttpPost();
				gson = new Gson();
		    }
		
		    public static RestFulServiceManager getInstance() {
		        if (instance == null) {
		            instance = new RestFulServiceManager();
		        }
		        return instance;
			}
		
		    public void shutDownService() {
		        httpClient.getConnectionManager().shutdown();
		    }
		    
				
		public LiveLocationFeed sendAthleteLocationInfo(int userId, int athleteId, double lat, double lng, double height, double speed) 
			throws NetworkException, ServerException {
			try {
				String restfulURL = Klimatur_URL + "/restfulservice/liveLocation/athlete";
				List<NameValuePair> data = new ArrayList<NameValuePair>();
								
				httppost.setURI(new URI(restfulURL));	
					data.add(new BasicNameValuePair("userId", String.valueOf(userId)));
					data.add(new BasicNameValuePair("athleteId", String.valueOf(athleteId)));
					data.add(new BasicNameValuePair("lat", String.valueOf(lat)));
					data.add(new BasicNameValuePair("lng", String.valueOf(lng)));
					data.add(new BasicNameValuePair("height", String.valueOf(height)));
					data.add(new BasicNameValuePair("speed", String.valueOf(speed)));
				httppost.setEntity(new UrlEncodedFormEntity(data));
	            HttpResponse response = httpClient.execute(httppost);
				            	            
	            int statusCode = response.getStatusLine().getStatusCode();	            
	            if (statusCode != 200 && statusCode != 201) {
	                String errorString = response.getStatusLine().getReasonPhrase();
	                throw new ServerException(errorString, statusCode);
	            }
	             else {
					String resultString = Utility.getResultFromHttpResponse(response);
						return gson.fromJson(resultString, LiveLocationFeed.class);
				}
	        } catch (Exception ex) {
	            throw new NetworkException(ex.getMessage());
        	}
		}
	
		public LiveLocationFeed sendSupportLocationInfo(int userId, int supportId, double lat, double lng, double height, double speed) 
			throws NetworkException, ServerException {
			try {
				String restfulURL = Klimatur_URL + "/restfulservice/liveLocation/support";
				List<NameValuePair> data = new ArrayList<NameValuePair>();
								
				httppost.setURI(new URI(restfulURL));	
					data.add(new BasicNameValuePair("userId", String.valueOf(userId)));
					data.add(new BasicNameValuePair("supportId", String.valueOf(supportId)));
					data.add(new BasicNameValuePair("lat", String.valueOf(lat)));
					data.add(new BasicNameValuePair("lng", String.valueOf(lng)));
					data.add(new BasicNameValuePair("height", String.valueOf(height)));
					data.add(new BasicNameValuePair("speed", String.valueOf(speed)));
				httppost.setEntity(new UrlEncodedFormEntity(data));
	            HttpResponse response = httpClient.execute(httppost);
				            	            
	            int statusCode = response.getStatusLine().getStatusCode();	            
	            if (statusCode != 200 && statusCode != 201) {
	                String errorString = response.getStatusLine().getReasonPhrase();
	                throw new ServerException(errorString, statusCode);
	            }
	             else {
					String resultString = Utility.getResultFromHttpResponse(response);
						return gson.fromJson(resultString, LiveLocationFeed.class);
				}
	        } catch (Exception ex) {
	            throw new NetworkException(ex.getMessage());
        	}
		}
	
		public LiveMediaFeed sendAthleteMedia(int userId, int athleteId, double lat, double lng, double height, double speed) 
			throws NetworkException, ServerException {
			try {
				String restfulURL = Klimatur_URL + "/restfulservice/liveMedia/athlete";
				List<NameValuePair> data = new ArrayList<NameValuePair>();
								
				httppost.setURI(new URI(restfulURL));	
					data.add(new BasicNameValuePair("userId", String.valueOf(userId)));
					data.add(new BasicNameValuePair("athleteId", String.valueOf(athleteId)));
					data.add(new BasicNameValuePair("lat", String.valueOf(lat)));
					data.add(new BasicNameValuePair("lng", String.valueOf(lng)));
					data.add(new BasicNameValuePair("height", String.valueOf(height)));
					data.add(new BasicNameValuePair("speed", String.valueOf(speed)));
				httppost.setEntity(new UrlEncodedFormEntity(data));
	            HttpResponse response = httpClient.execute(httppost);
				            	            
	            int statusCode = response.getStatusLine().getStatusCode();	            
	            if (statusCode != 200 && statusCode != 201) {
	                String errorString = response.getStatusLine().getReasonPhrase();
	                throw new ServerException(errorString, statusCode);
	            }
	             else {
					String resultString = Utility.getResultFromHttpResponse(response);
						return gson.fromJson(resultString, LiveMediaFeed.class);
				}
	        } catch (Exception ex) {
	            throw new NetworkException(ex.getMessage());
        	}
		}
	
		public LiveMediaFeed sendSupportLocationInfo(int userId, int supportId, double lat, double lng, double height, double speed) 
			throws NetworkException, ServerException {
			try {
				String restfulURL = Klimatur_URL + "/restfulservice/liveMedia/support";
				List<NameValuePair> data = new ArrayList<NameValuePair>();
								
				httppost.setURI(new URI(restfulURL));	
					data.add(new BasicNameValuePair("userId", String.valueOf(userId)));
					data.add(new BasicNameValuePair("supportId", String.valueOf(supportId)));
					data.add(new BasicNameValuePair("lat", String.valueOf(lat)));
					data.add(new BasicNameValuePair("lng", String.valueOf(lng)));
					data.add(new BasicNameValuePair("height", String.valueOf(height)));
					data.add(new BasicNameValuePair("speed", String.valueOf(speed)));
				httppost.setEntity(new UrlEncodedFormEntity(data));
	            HttpResponse response = httpClient.execute(httppost);
				            	            
	            int statusCode = response.getStatusLine().getStatusCode();	            
	            if (statusCode != 200 && statusCode != 201) {
	                String errorString = response.getStatusLine().getReasonPhrase();
	                throw new ServerException(errorString, statusCode);
	            }
	             else {
					String resultString = Utility.getResultFromHttpResponse(response);
						return gson.fromJson(resultString, LiveMediaFeed.class);
				}
	        } catch (Exception ex) {
	            throw new NetworkException(ex.getMessage());
        	}
		}
	
		public UserInfo getUserInfo(String username, int routeId) 
			throws NetworkException, ServerException {
			try {
				String restfulURL = Klimatur_URL + "/restfulservice/getUserInfo";
				List<NameValuePair> data = new ArrayList<NameValuePair>();
								
				httppost.setURI(new URI(restfulURL));	
					data.add(new BasicNameValuePair("username", String.valueOf(username)));
					data.add(new BasicNameValuePair("routeId", String.valueOf(routeId)));
				httppost.setEntity(new UrlEncodedFormEntity(data));
	            HttpResponse response = httpClient.execute(httppost);
				            	            
	            int statusCode = response.getStatusLine().getStatusCode();	            
	            if (statusCode != 200 && statusCode != 201) {
	                String errorString = response.getStatusLine().getReasonPhrase();
	                throw new ServerException(errorString, statusCode);
	            }
	             else {
					String resultString = Utility.getResultFromHttpResponse(response);
						return gson.fromJson(resultString, UserInfo.class);
				}
	        } catch (Exception ex) {
	            throw new NetworkException(ex.getMessage());
        	}
		}
	
		public RouteInfoCollection getRouteInfo(String username) 
			throws NetworkException, ServerException {
			try {
				String restfulURL = Klimatur_URL + "/restfulservice/routeInfo";
				List<NameValuePair> data = new ArrayList<NameValuePair>();
								
				httppost.setURI(new URI(restfulURL));	
					data.add(new BasicNameValuePair("username", String.valueOf(username)));
				httppost.setEntity(new UrlEncodedFormEntity(data));
	            HttpResponse response = httpClient.execute(httppost);
				            	            
	            int statusCode = response.getStatusLine().getStatusCode();	            
	            if (statusCode != 200 && statusCode != 201) {
	                String errorString = response.getStatusLine().getReasonPhrase();
	                throw new ServerException(errorString, statusCode);
	            }
	             else {
					String resultString = Utility.getResultFromHttpResponse(response);
						return gson.fromJson(resultString, RouteInfoCollection.class);
				}
	        } catch (Exception ex) {
	            throw new NetworkException(ex.getMessage());
        	}
		}
	
		public TargetInfoPacket getNextTargetInfo(int athleteId) 
			throws NetworkException, ServerException {
			try {
				String restfulURL = Klimatur_URL + "/restfulservice/nexttargetinfo";
				List<NameValuePair> data = new ArrayList<NameValuePair>();
								
				httppost.setURI(new URI(restfulURL));	
					data.add(new BasicNameValuePair("athleteId", String.valueOf(athleteId)));
				httppost.setEntity(new UrlEncodedFormEntity(data));
	            HttpResponse response = httpClient.execute(httppost);
				            	            
	            int statusCode = response.getStatusLine().getStatusCode();	            
	            if (statusCode != 200 && statusCode != 201) {
	                String errorString = response.getStatusLine().getReasonPhrase();
	                throw new ServerException(errorString, statusCode);
	            }
	             else {
					String resultString = Utility.getResultFromHttpResponse(response);
						return gson.fromJson(resultString, TargetInfoPacket.class);
				}
	        } catch (Exception ex) {
	            throw new NetworkException(ex.getMessage());
        	}
		}
	
		public TeamMemberLocationPacket getSupportsLocation(int athleteId) 
			throws NetworkException, ServerException {
			try {
				String restfulURL = Klimatur_URL + "/restfulservice/supportteammemberlocation";
				List<NameValuePair> data = new ArrayList<NameValuePair>();
								
				httppost.setURI(new URI(restfulURL));	
					data.add(new BasicNameValuePair("athleteId", String.valueOf(athleteId)));
				httppost.setEntity(new UrlEncodedFormEntity(data));
	            HttpResponse response = httpClient.execute(httppost);
				            	            
	            int statusCode = response.getStatusLine().getStatusCode();	            
	            if (statusCode != 200 && statusCode != 201) {
	                String errorString = response.getStatusLine().getReasonPhrase();
	                throw new ServerException(errorString, statusCode);
	            }
	             else {
					String resultString = Utility.getResultFromHttpResponse(response);
						return gson.fromJson(resultString, TeamMemberLocationPacket.class);
				}
	        } catch (Exception ex) {
	            throw new NetworkException(ex.getMessage());
        	}
		}
	
		public GeoPoint getLatestLocationOfAthlete(int athleteId) 
			throws NetworkException, ServerException {
			try {
				String restfulURL = Klimatur_URL + "/restfulservice/athleteLatestLocation";
				List<NameValuePair> data = new ArrayList<NameValuePair>();
								
				httppost.setURI(new URI(restfulURL));	
					data.add(new BasicNameValuePair("athleteId", String.valueOf(athleteId)));
				httppost.setEntity(new UrlEncodedFormEntity(data));
	            HttpResponse response = httpClient.execute(httppost);
				            	            
	            int statusCode = response.getStatusLine().getStatusCode();	            
	            if (statusCode != 200 && statusCode != 201) {
	                String errorString = response.getStatusLine().getReasonPhrase();
	                throw new ServerException(errorString, statusCode);
	            }
	             else {
					String resultString = Utility.getResultFromHttpResponse(response);
						return gson.fromJson(resultString, GeoPoint.class);
				}
	        } catch (Exception ex) {
	            throw new NetworkException(ex.getMessage());
        	}
		}

		}
	